#!/bin/bash

# Easy-MQ Demo API 测试脚本
# 使用方法: ./test-api.sh

BASE_URL="http://localhost:9001"

echo "🚀 Easy-MQ Demo API 测试开始..."
echo "=================================="

# 1. 测试健康检查
echo "1. 测试健康检查..."
curl -s "$BASE_URL/actuator/health" | jq '.'
echo ""

# 2. 创建订单
echo "2. 创建订单..."
ORDER_RESPONSE=$(curl -s -X POST "$BASE_URL/api/demo/orders" \
  -H 'Content-Type: application/json' \
  -d '{
    "userId": "user001",
    "productId": "prod001",
    "productName": "iPhone 15",
    "amount": 9999.99
  }')

echo "$ORDER_RESPONSE" | jq '.'
ORDER_ID=$(echo "$ORDER_RESPONSE" | jq -r '.order.orderId')
echo ""

# 3. 查看库存
echo "3. 查看库存..."
curl -s "$BASE_URL/api/demo/inventory" | jq '.'
echo ""

# 4. 更新订单状态
echo "4. 更新订单状态..."
curl -s -X PUT "$BASE_URL/api/demo/orders/$ORDER_ID/status" \
  -H 'Content-Type: application/json' \
  -d '{"status": "PAID"}' | jq '.'
echo ""

# 5. 查看统计信息
echo "5. 查看统计信息..."
curl -s "$BASE_URL/api/demo/stats" | jq '.'
echo ""

# 6. 取消订单
echo "6. 取消订单..."
curl -s -X DELETE "$BASE_URL/api/demo/orders/$ORDER_ID" \
  -H 'Content-Type: application/json' \
  -d '{"reason": "测试取消"}' | jq '.'
echo ""

# 7. 再次查看库存（应该恢复）
echo "7. 再次查看库存（应该恢复）..."
curl -s "$BASE_URL/api/demo/inventory" | jq '.'
echo ""

# 8. 批量创建订单
echo "8. 批量创建订单..."
curl -s -X POST "$BASE_URL/api/demo/orders/batch" \
  -H 'Content-Type: application/json' \
  -d '{
    "count": 3,
    "userId": "user002",
    "productId": "prod002",
    "productName": "MacBook Pro",
    "amount": 19999.99
  }' | jq '.'
echo ""

# 9. 最终统计
echo "9. 最终统计信息..."
curl -s "$BASE_URL/api/demo/stats" | jq '.'
echo ""

echo "✅ API 测试完成！"
echo "=================================="
echo "📊 健康检查: $BASE_URL/actuator/health"
echo "🎛️  Web控制台: $BASE_URL/easymq"
echo "📈  Prometheus指标: $BASE_URL/actuator/prometheus" 